import React from 'react';

class LiftingStateUpUnit extends React.Component{

	constructor(props){
		super(props);
		//constructor 只会执行一次
		//父重新render 之后，此constructor 不会被重新调用，但props 会改为新的，
		//因此此处赋值，有不会变化，引用父属性应直接用this.props.属性名
		//this.unit=props.unit; 
		//this.value = props.value;
		this.parentSetValueHandler = props.parentSetValueHandler;
		this.getValueHandler = this.getValue.bind(this);
		this.setValueHandler = this.setValue.bind(this);
		
	}

	setValue(event){
		let value = event.target.value;
		let v = parseInt(value);
		let v1 = this.setConvert(v);
		 
		//console.log("setValue:"+v1);
		this.parentSetValueHandler(v1);
	}
	getValue (){
		let v = parseInt(this.props.value);
		let v1 =  this.getConvert(v);
		//console.log("getValue:"+v1);
		return v1;
	}

	setConvert(value){
		let v = 0;
		if(this.props.unit==='元'){
			v =  value *100;
		}else if(this.props.unit==='角'){
			v =  value *10;
		}else{
			v =  value;
		}
		return v;
	}
	getConvert(value){
		let v = 0;
		if(this.props.unit==='元'){
			v =  value / 100;
		}else if(this.props.unit==='角'){
			v =  value /10;
		}else{
			v =  value;
		}
		return v;
	}
	render(){
		return (
			<label>
				{this.props.unit}:<input type="text" value={this.getValueHandler()} onChange={this.setValueHandler}/>
			</label>
		);
	}
}
export default LiftingStateUpUnit;